﻿using BCT.Models;
using BCT.Repositories.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCT.Repositories
{
    public class SkillDetailManRepository : ISkillDetailManRepository
    {
        //获取技能详情列表
        public List<SkillDetailModel> GetSkillDetailList(string skilldetailname,int skillgroupId) {
            using (var context = new EvaluateEntities()) {
                if (string.IsNullOrEmpty(skilldetailname) && skillgroupId == 0)
                {
                    var skillDetailList = context.SkillDetail
                               //.Where(s => s.UserPower == 1)
                               .Select(s => new SkillDetailModel()
                               {
                                   SkillDetailId = s.SkillDetailId,
                                   SkillDetailName = s.SkillDetailName,
                                   SkillDescription = s.SkillDescription,
                                   SkillGroupId = s.SkillGroupId,
                                   SkillGroupName = s.SkillGroup.SkillGroupName,
                                   Active = s.Active
                               }).ToList();
                    return skillDetailList;
                }
                else if ((!string.IsNullOrEmpty(skilldetailname)) && (!(skillgroupId == 0)))
                {
                    var skillDetailList = context.SkillDetail
                             .Where(s => s.SkillDetailName.Contains(skilldetailname) && s.SkillGroupId == skillgroupId)
                             .Select(s => new SkillDetailModel()
                             {
                                 SkillDetailId = s.SkillDetailId,
                                 SkillDetailName = s.SkillDetailName,
                                 SkillDescription = s.SkillDescription,
                                 SkillGroupId = s.SkillGroupId,
                                 SkillGroupName = s.SkillGroup.SkillGroupName,
                                 Active = s.Active
                             }).ToList();
                    return skillDetailList;
                }
                else
                {
                    var skillDetailList = context.SkillDetail
                               .Where(s => s.SkillDetailName.Contains(skilldetailname) || s.SkillGroupId == skillgroupId)
                               .Select(s => new SkillDetailModel()
                               {
                                   SkillDetailId = s.SkillDetailId,
                                   SkillDetailName = s.SkillDetailName,
                                   SkillDescription = s.SkillDescription,
                                   SkillGroupId = s.SkillGroupId,
                                   SkillGroupName = s.SkillGroup.SkillGroupName,
                                   Active = s.Active
                               }).ToList();
                    return skillDetailList;
                }
            }
        }
        //添加技能详情
        public bool AddSkillDetail(SkillDetailModel model)
        {
            using (var context = new EvaluateEntities())
            {
                var skillDetail = context.SkillDetail.FirstOrDefault(s => s.SkillDetailName == model.SkillDetailName);
                if (skillDetail != null)
                {
                    return false;
                }
                else
                {
                    context.SkillDetail.Add(new SkillDetail()
                    {
                        SkillDetailName = model.SkillDetailName,
                        SkillGroupId = model.SkillGroupId,
                        SkillDescription = model.SkillDescription
                    });
                    return context.SaveChanges() > 0;
                }
            }
        }
        //禁用或启用技能详情
        public string LogDelete(SkillDetailModel model)
        {
            using (var context = new EvaluateEntities())
            {
                var skillDetail = context.SkillDetail.FirstOrDefault(s => s.SkillDetailId == model.SkillDetailId);
                skillDetail.Active = model.Active;
                if(context.SaveChanges() > 0)
                {
                    return "修改成功！";
                }
                else
                {
                    return "修改失败！";
                }
            }
        }
        //修改技能详情
        public string ModefySkillDetail(SkillDetailModel model)
        {
            using (var context = new EvaluateEntities()) {
                var ifhaveSkillDetail = context.SkillDetail.FirstOrDefault(s => s.SkillDetailName == model.SkillDetailName && s.SkillDetailId != model.SkillDetailId);
                if (ifhaveSkillDetail != null)
                {
                    return "该技能详情已存在！";
                }
                else {
                    var skillDetail = context.SkillDetail.FirstOrDefault(s => s.SkillDetailId == model.SkillDetailId);
                    skillDetail.SkillDetailName = model.SkillDetailName;
                    skillDetail.SkillGroupId = model.SkillGroupId;
                    skillDetail.SkillDescription = model.SkillDescription;
                    if (context.SaveChanges() > 0)
                    {
                        return "修改成功！";
                    }
                    else
                    {
                        return "修改失败！";
                    }
                }
            }
        }
    }
}
